package com.netty.cn.rpc.selfmessage;

import com.netty.cn.rpc.selfmessage.core.MyMessage;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class MyServerHandler extends SimpleChannelInboundHandler<MyMessage> {
    private int count;
    /**
     * 读取客户端的数据
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyMessage myMessage) throws Exception {
        System.out.println("服务端收到消息：");
        System.out.println("长度："+myMessage.getLen());
        System.out.println("内容: "+new String(myMessage.getContent(),CharsetUtil.UTF_8));
        System.out.println("收到消息数量："+(++count));
        
        String msg="服务端收到请求";
        MyMessage message=new MyMessage();
        message.setContent(msg.getBytes(CharsetUtil.UTF_8));
        message.setLen(msg.getBytes(CharsetUtil.UTF_8).length);
        ctx.writeAndFlush(message);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    	super.channelReadComplete(ctx);
    	// 客户端连接进入 FIN_WAIT1 状态
    	//    ctx.channel().close();
    }
}
